<?php

class Application_Model_HistoSystemElement {

	protected $idAction;
	protected $user;
	protected $group;
	protected $time;
	protected $libelle;

	public function setIdAction($id) {
		$this->idAction = $id;
		return $this;
	}

	public function getIdAction() {
		return $this->idAction;
	}

	public function setUser($user) {
		$this->user = $user;
		return $this;
	}

	public function getUser() {
		return $this->user;
	}

	public function setGroup($group) {
		$this->group = $group;
		return $this;
	}

	public function getGroup() {
		return $this->group;
	}

	public function setTime($time) {
		$this->time = $time;
		return $this;
	}

	public function getTime() {
		return $this->time;
	}

	public function setLibelle($libelle) {
		$this->libelle = $libelle;
		return $this;
	}

	public function getLibelle() {
		return $this->libelle;
	}

	public function Application_Model_HistoSystemElement($libelle = '') {
		$this->libelle = $libelle;
	}

	public function AddToHistoSystem()
	{
		$db = new Application_Model_HistoSystemTable();
		$session = new Zend_Session_Namespace("codebar");
		$data = array(
			'login' => $session->user->getLogin(),
			'libelle' => $this->libelle,
			'id_groupe' => $session->user->getGroupeActif()->getID(),
			'time' => date('Y-m-d H:i:s')
		);
		$db->insert($data);
	}

	/**
	 *
	 * Search for the system actions by $user between $date1 and $date2.
	 * @param unknown_type $date1
	 * @param unknown_type $date2
	 * @param Application_Model_User $user
	 * @return An array of Application_Model_HistoSystemElement.
	 */
	public static function searchHistoSystem($date1 = '',$date2 = '',$user = '') {
		$db = new Application_Model_HistoSystemTable();
		if($user == '') {
			$rows = $db->fetchAll($db->select()->where('time < ?',$date2)->where('time > ?',$date1)->order('id_action DESC'));
		} else {
			$rows = $db->fetchAll($db->select()->where('time < ?',$date2)->where('time > ?',$date1)->where('login = ?',$user->getLogin())->order('id_action DESC'));
		}
		$result = array();
		foreach($rows as $row) {
			$result[] = Application_Model_HistoSystemElement::getFromRow($row);
		}
		return $result;
	}

	/**
	 *
	 * Transforms a row of the table into an instance.
	 * @param unknown_type $row
	 * @return Application_Model_HistoSystemElement
	 */
	public static function getFromRow($row) {
		$element = new Application_Model_HistoSystemElement();
		$element->setIdAction($row['id_action'])
		->setGroup(Application_Model_Groupe::getByIdGroup($row['id_groupe']))
		->setUser(Application_Model_User::getByLogin($row['login']))
		->setTime($row['time'])
		->setLibelle($row['libelle']);
		return $element;
	}

	/**
	 *
	 * Return a string representif the $attrib.
	 * @param String $attrib
	 * @return String representation of $attrib
	 */
	public function printv($attrib) {
		switch($attrib)
		{
			case "time":
				/*
				 * Some elements in the database may be of the form
				 * 2012-04-09 20:40:54.482452
				 * The miliseconds mess up the date sorting in
				 * dataTables.
				 */
				$time = substr($this->getTime(), 0, 19);
				$date = DateTime::createFromFormat('Y-m-d H:i:s', $time);
				echo $date->format("d/m/Y H:i:s");
				break;
			case "user":
				echo $this->user->getLogin();
				break;
			case "libelle":
				echo $this->libelle;
				break;
			case "idAction":
				echo $this->idAction;
				break;
			case "group":
				if(isset($this->groupe)) {
					echo $this->group->getName();
				} else {
					echo "N/A";
				}
				break;
		}
	}
}